<template>
  <div>


    <basic-container>
      <vue-element-loading :active="isActive" spinner="bar-fade-scale" color="#409eff" background-color="rgba(255,255,255,0.2)"/>
      <el-form :inline="true" :model="formReport" >
        <div style="margin-top: 22px">
          <el-form-item>
            <el-input v-model="formReport.account" clearable placeholder="账号"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary"  @click="onLoad">查询</el-button>
            <el-button icon="el-icon-edit" type="primary"  @click="addWhiteList">添加账号</el-button>
            <el-button icon="el-icon-edit" type="primary"  @click="showWhiteDeviceList">设备白名单</el-button>
          </el-form-item>

        </div>
      </el-form>
      <el-row>
        <el-col :span="24">
          <template>
            <el-table
              :data="tableData"
              style="width: 100%">
              <el-table-column
                prop="ipCity"
                label="IP归属">
              </el-table-column>
              <el-table-column
                prop="account"
                label="账号">
              </el-table-column>

              <el-table-column
                width="200"
                label="操作">
                <template slot-scope="scope" align="center">
                  <div>
                    <el-button @click="editW(scope.row)" type="text" size="medium">编辑</el-button>
                    <el-button @click="removeW(scope.row.account)" type="text" size="medium">删除</el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.currentPage"
            :page-sizes="[10, 20, 50, 100, 200]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
          </el-pagination>
        </el-col>
      </el-row>

      <el-dialog :title="mode" :visible.sync="dialogFormVisible" @close="closeDialog()" :close-on-click-modal="false" customClass="customWidthCompany">
        <div v-if="dialogFormVisible" style="text-align: center">
          <el-form :inline="true" :model="formGrant" :rules="rules" label-width="150px" class="demo-form-inline" ref="grantForm" style="margin-top: 25px">
            <el-row>
              <el-col>
                <el-form-item label="IP归属：" prop="ipCity">
                  <el-input placeholder="IP归属" v-model="formGrant.ipCity" clearable class="input-with-select" style="width:350px">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item label="账号：" prop="account">
                  <el-input placeholder="一行一个账号" type="textarea" :rows="6" v-model="formGrant.account" clearable class="input-with-select" style="width:350px">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeDialog" style="width: 150px">取 消</el-button>
          <el-button v-loading.fullscreen.lock="fullscreenLoading" type="primary" @click="suer('grantForm')" style="width: 150px">确 认</el-button>
        </div>
      </el-dialog>

      <el-dialog :title="mode" :visible.sync="dialogEditFormVisible" @close="closeEditDialog()" :close-on-click-modal="false" customClass="customWidthCompany">
        <div v-if="dialogEditFormVisible" style="text-align: center">
          <el-form :inline="true" :model="formEditGrant" :rules="rules" label-width="150px" class="demo-form-inline" ref="grantEdit" style="margin-top: 25px">
            <el-row>
              <el-col>
                <el-form-item label="账号：" prop="account">
                  <el-input placeholder="账号" v-model="formEditGrant.account" clearable class="input-with-select" :disabled="true" style="width:350px">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item label="IP归属：" prop="ipCity">
                  <el-input placeholder="IP归属" v-model="formEditGrant.ipCity" clearable class="input-with-select" style="width:350px">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>

          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeEditDialog" style="width: 150px">取 消</el-button>
          <el-button v-loading.fullscreen.lock="fullscreenLoading" type="primary" @click="editData('grantEdit')" style="width: 150px">确 认</el-button>
        </div>
      </el-dialog>

      <el-dialog :title="mode" :visible.sync="dialogDeviceFormVisible" @close="closeDeviceDialog()" :close-on-click-modal="false" customClass="customWidthCompany">
        <div v-if="dialogDeviceFormVisible" style="text-align: center">
          <el-form :inline="true" :model="formDeviceGrant" label-width="150px" class="demo-form-inline" ref="grantDevice" style="margin-top: 25px">
            <el-row>
              <el-col>
                <el-form-item label="设备IMEI：" prop="account">
                  <el-input placeholder="设备IMEI" type="textarea" :rows="15" v-model="formDeviceGrant.devices" clearable class="input-with-select" style="width:650px">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeDeviceDialog" style="width: 150px">取 消</el-button>
          <el-button v-loading.fullscreen.lock="fullscreenLoading" type="primary" @click="editDeviceData('grantDevice')" style="width: 150px">确 认</el-button>
        </div>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>

import {mapGetters} from "vuex";
import VueElementLoading from 'vue-element-loading'
import {
  addLoginWhiteList,
  loginWhiteList,
  removeLoginWhiteList,
  editLoginWhiteList,
  editLoginDeviceWhiteList,
  loadLoginDeviceWhiteList
} from "@/api/game/gameDeposit";
export default {
  name: "headman",

  data() {
    return {
      isActive: false,
      dialogFormVisible:false,
      fullscreenLoading: false,
      dialogEditFormVisible:false,
      dialogDeviceFormVisible:false,
      formReport: {
        account:''
      },
      formGrant: {
        account:'',
        ipCity:'福州市',
      },
      formEditGrant: {
        account:'',
        ipCity:'',
      },
      formDeviceGrant:{
        devices:''
      },
      page: {
        currentPage: 1,
        pageSize: 20,
        total: '',
      },
      totalCount:'',
      tableData:[],
      tableTotal:{},
      rules: {
        account:[
          {required: true, message: '请输入账号', trigger: 'blur'}
        ]
      }
    };

  },
  components: {
    VueElementLoading
  },
  created() {
    this.onLoad()
  },
  methods: {
    editW(row){
      this.dialogEditFormVisible = true;
      this.mode = '编辑白名单';

      this.formEditGrant.account = row.account
      this.formEditGrant.ipCity = row.ipCity
    },
    editData(grantEditForm){
      this.$refs[grantEditForm].validate((valid) => {
        if (valid) {
          editLoginWhiteList(this.formEditGrant).then(res => {
            this.loading = false;
            this.formEditGrant = {
              account:'',
              ipCity:'',
            }
            if (res.data.success) {
              this.dialogEditFormVisible=false
              this.$message({
                type: "success",
                message: "操作成功!"
              });
              this.onLoad();
            } else {
              this.$message({
                type: "error",
                message: res.data.msg
              });
            }
          })
        }
      })
    },
    getLoginDeviceWhiteList() {
      loadLoginDeviceWhiteList().then(res => {
        if (res.data.success) {
          this.formDeviceGrant.devices = res.data.data
        } else {

        }
      })
    },
    editDeviceData(){
      editLoginDeviceWhiteList(this.formDeviceGrant.devices).then(res => {
        if (res.data.success) {
          this.$message({
            type: "success",
            message: "编辑成功!"
          });
          this.closeDeviceDialog()
        } else {
          this.$message({
            type: "error",
            message: res.data.msg
          });
        }
      })
    },
    closeDeviceDialog(){
      this.dialogDeviceFormVisible = false;
      this.formDeviceGrant.devices = '';
    },
    showWhiteDeviceList(){
      this.dialogDeviceFormVisible = true;
      this.mode = '设备白名单';
      this.getLoginDeviceWhiteList();
    },
    closeEditDialog() {
      this.dialogEditFormVisible = false
      this.formEditGrant = {
        account:'',
        ipCity:'',
      }
    },
    removeW(account) {
      this.$confirm("确定要删除该信息吗？", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeLoginWhiteList(account).then(res => {
          if (res.data.success) {
            this.$message({
              type: "success",
              message: "删除信息成功!"
            });
            this.onLoad();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg
            });
          }
        })
      }).catch(() => {


      });
    },
    suer(grantForm) {
      this.$refs[grantForm].validate((valid) => {
        if (valid) {
          addLoginWhiteList(this.formGrant).then(res => {
            this.loading = false;
            this.formGrant = {
              account:'',
              ipCity:'福州市',
            }
            if (res.data.success) {
              this.dialogFormVisible=false
              this.$message({
                type: "success",
                message: "操作成功!"
              });
              this.onLoad();
            } else {
              this.$message({
                type: "error",
                message: res.data.msg
              });
            }
          })
        }
      })
    },
    addWhiteList(){
      this.dialogFormVisible = true;
      this.mode = '添加白名单';
    },
    closeDialog() {
      this.dialogFormVisible = false
      this.formGrant = {
        account:'',
        ipCity:'福州市',
      }
    },
    isNotEmpty(arr) {
      return arr != null && arr.length !== 0
    },
    onLoad(){
      this.isActive = true;
      loginWhiteList(this.page.currentPage, this.page.pageSize, Object.assign(this.formReport)).then(res => {
        const data = res.data.data;
        console.log("res.data.data", res.data.data)
        this.page.total = data.total;
        this.tableData = data.records;
        this.isActive = false;
      })
    },
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.onLoad();
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.onLoad();
    }
  }
};
</script>

<style lang="scss" >

</style>
